<?php
/**
 * Paypal API
 *
 * This package provides access to the Paypal api
 *
 * @package		Clear Media Package
 * @author		Clear Media UK Ltd Dev Team
 * @copyright	Copyright (c) 2008 Clear Media UK.
 * @license		
 * @link		http://clearmediawebsites.co.uk
 * @since		Version 1.0
 * @filesource
 */
/**
 * This low-level class is responsible for actually communicating with Paypal
 * it ensures requests are understandable by Paypal, prefixing them with usernames / passwords 
 * etc
 *
 * @author		Clear Media UK Ltd Dev Team
 * @copyright	Copyright (c) 2008 Clear Media UK.
 * @license		
 * @link		http://clearmediawebsites.co.uk
 * @since		Version 1.0
 */
class Paypal_api_transport
{
	protected $mode					= 'SANDBOX';	// If this says live, then the system is live! anything else and we're in test.
	private $sandbox_server 		= 'https://api-3t.sandbox.paypal.com/nvp'; 
	private $live_server			= 'https://api-3t.paypal.com/nvp';
	protected $current_server		= '';
	
	protected $api_username			= '';
	protected $api_password			= '';
	protected $api_signature		= '';
	
	private $sandbox_api_username	= 'chris_api1.clearmediawebsites.co.uk';
	private $sandbox_api_password	= 'YYYDZLHXL7UNU8LW';
	private $sandbox_api_signature	= 'AJAtESTI1dB8XhdS4BNBjUa4oNMNAjrXdxKu2chmqnGDVWA.2Y3fj4tQ';	
	
	protected $api_version			= '63.0';
	
	protected $api_response_token 	= '';
	protected $debug				= false;
	
	// For reading the various response fields
	protected $result				= '';
	protected $result_array			= array();
	protected $error				= '';
	protected $profile_id			= '';
	protected $payer_id				= '';

	/**
	 * constructor, basically sets up the API endpoints after checking
	 * if cURL is installed
	 *
	 * @param void
	 * @return void
	 */
	public function __construct()
	{
		if( ! $this->_is_curl_installed()) {
			die('You need the cURL PHP extension installed to use this library, speak to your hosting company to get it installed.');
		}
				
		$this->mode = strtoupper($this->mode);
		
		if($this->mode == 'LIVE') {
			$this->current_server = $this->live_server;
		} else {
			$this->current_server 	= $this->sandbox_server;
			$this->api_username 	= $this->sandbox_api_username;
			$this->api_password 	= $this->sandbox_api_password;
			$this->api_signature 	= $this->sandbox_api_signature;
		}		
	}
	
    // --------------------------------------------   
	
	/**
	 * responsible for actually formatting and then sending the NVP requests to Paypal
	 *
	 * @param string
	 * @param string
	 * @return bool
	 * @todo make this more elegant, the method of setting some of the requested details, such as PAYERID is kinda rubbish
	 */
	protected function execute_request($method = '', $data = '')
	{
		// Convert the data to a correctly formatted NVP string	
		$data = http_build_query($data, '', '&');
		
		$data = 'METHOD=' . $method . '&VERSION=' . urlencode($this->api_version) . '&PWD=' . urlencode($this->api_password) . 
				'&USER=' . urlencode($this->api_username) . '&SIGNATURE=' . urlencode($this->api_signature) . 
				'&' . $data;
		
		$handle = curl_init();		
		curl_setopt($handle, CURLOPT_URL, $this->current_server);
		curl_setopt($handle, CURLOPT_VERBOSE, 1);
		
		curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, FALSE);		
		
		curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);		
		curl_setopt($handle, CURLOPT_POST, 1);

		curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
		
	
		$result = curl_exec($handle);				
		curl_close($handle);
		
		parse_str($result, $result);		
		$this->api_response_token 	= @$result['TOKEN'];
		$this->result				= $result['ACK'];
		$this->result_array			= $result;
		
		if(strtolower($this->result) == 'failure') {
			$this->error = $result['L_SHORTMESSAGE0'] . ' - Addition error message: ' . $result['L_LONGMESSAGE0'];
		}
		
		if(isset($result['PROFILEID'])) {
			$this->profile_id = $result['PROFILEID'];
		}

		if(isset($result['PAYERID'])) {
			$this->payer_id = $result['PAYERID'];
		}		
		
		if($this->debug == true) {
			echo $data . '<br /><br />';
			echo 'Current Server: ' . $this->current_server . '<br />';		
			echo $result['ACK'] . '<br />';
			echo '<pre>';
			echo print_r($result);
			echo '</pre>';
		}
	}
	
    // -------------------------------------------- 
	

	/**
	 * checks if cURL is installed
	 *
	 * @param void
	 * @return bool
	 */
	private function _is_curl_installed()
	{
		if(in_array('curl', get_loaded_extensions())) {
			return true;
		} else {
			return false;
		}
	}
	
    // --------------------------------------------     	
	
}
